<template>
    <rx-dialog @ok="transfer" @cancel="closeWindow" :okText="$ti18('rxForm.commitBtn','提交')" :btnAlign="'center'">
        <rx-fit>
            <template #middle>
                <div style="height:100%;width:100%;padding:10px;">
                    <a-form :model="form" layout="vertical" class="rx-a-form-vertical">
                        <a-form-item :label="i18n('forwardList.forward','抄送人')" v-if="send">
                            <rx-user-component v-model="copyUsers" :isAccount="false"
                                               :single="false"></rx-user-component>
                        </a-form-item>
                        <a-form-item :label="i18n('forwardList.opinion','意见')">
                            <a-textarea v-model:value="form.opinion"></a-textarea>
                        </a-form-item>
                    </a-form>
                </div>
            </template>
        </rx-fit>
    </rx-dialog>
</template>
<script>

import {RxDialog, Util, RxFit, RxTextList} from '@lowcode/jpaas-base-lib';
import {RxUserComponent, RxAttachComponent} from '@lowcode/jpaas-share-component';
import BpmPublicApi from '@/common/api/bpm/BpmPublicApi';
import BpmInstCcApi from '@/common/api/bpm/bpmInstCc';

export default {
    name: 'bpm-task-transfer',
    components: {
        RxUserComponent,
        RxAttachComponent,
        RxFit,
        RxDialog,
        RxTextList
    },
    props: {
        layerid: String,
        destroy: Function,
        instId: String,
        send: {
            type: Boolean,
            default: true
        }

    },
    data() {
        return {

            labelCol: {span: 4},
            wrapperCol: {span: 8},
            msgOptions: [],

            copyUsers: [],
            files: [],
            msgTypes: [],
            form: {
                copyUsers: "",
                files: "[]",
                msgTypes: "",
                opinion: "",
                send: this.send,
                instId: this.instId
            }
        }
    },
    created() {
        this.initMessageHandler();
    },
    methods: {
        i18n(name, text, key) {
            return this.$ti18(name, text, "MyApprovedTasks", key);
        },
        initMessageHandler() {
            BpmPublicApi.getMessageHandler().then(res => {
                for (var i = 0; i < res.length; i++) {
                    var o = res[i];
                    var obj = {label: o.name, value: o.type};
                    this.msgOptions.push(obj);
                }
            })
        },
        transfer(vm) {
            vm.loading = false ;
            if (this.form.send) {
                if (this.copyUsers.length == 0) {
                    this.$message.warning(this.i18n('msa.selectExecutorTips', '请选择执行人!'));
                    return;
                }
            }


            this.form.copyUsers = JSON.stringify(this.copyUsers);
            this.form.files = JSON.stringify(this.files);
            this.form.msgTypes = JSON.stringify(this.msgTypes);

            BpmInstCcApi.transfer(this.form).then(res => {
                if (res.success) {
                    Util.closeWindow(this, 'ok');
                }
            });
        },
        closeWindow() {
            Util.closeWindow(this, 'cancel')
        },

    }
}
</script>
